package yanjunwei.rbac.service.impl;

import yanjunwei.rbac.dao.collection.IUserDao;
import yanjunwei.rbac.dao.collection.UserDao;
import yanjunwei.rbac.domain.dto.UserDto;
import yanjunwei.rbac.domain.entity.Dept;
import yanjunwei.rbac.domain.entity.Role;
import yanjunwei.rbac.domain.entity.User;
import yanjunwei.rbac.domain.vo.Result;
import yanjunwei.rbac.domain.vo.UserVo;
import yanjunwei.rbac.service.IDeptService;
import yanjunwei.rbac.service.IRoleService;
import yanjunwei.rbac.service.IUserService;

import yanjunwei.rbac.util.SingletonFactory;

import java.time.LocalDateTime;
import java.util.Collection;


public class UserService extends BaseService<User,UserDto,UserVo, IUserDao> implements IUserService {



    private final IDeptService deptService;
    private final IRoleService roleService;



    private UserService(){
        deptService=SingletonFactory.getInstance(DeptService.class);
        roleService=SingletonFactory.getInstance(RoleService.class);
        dao=SingletonFactory.getInstance(UserDao.class);
        temp=new UserVo[0];
    }



    @Override
    UserVo convertToVo(User user) {
        String deptName = "";
        Dept dept = deptService.findById(user.deptId);
        if (dept != null) {
            deptName = dept.name;
        }
        String roleName = "";
        Role role = roleService.findById(user.roleId);
        if (role != null) {
            roleName = role.name;
        }
        return new UserVo(user.getId(),user.createAt,user.updateAt, user.username, user.name, user.mobile, user.email, user.deptId, deptName,
                user.roleId, roleName, user.enabled);
    }
@Override
    User createEntity(UserDto dto) {
         //ctrl+p快捷键
    LocalDateTime now=LocalDateTime.now();
        return new User(null,now,now,dto.username,dto.name, dto.mobile, dto.email, dto.deptId, dto.roleId);
    }
@Override
    void updateEntity(UserDto dto, User user) {
        user.createAt=LocalDateTime.now();
        user.updateAt=LocalDateTime.now();
        user.username = dto.username;
        user.name = dto.name;
        user.mobile = dto.mobile;
        user.email = dto.email;
        user.roleId = dto.roleId;
    }
@Override
    public boolean deptInUse(int deptId){
        boolean inUse=false;
    Collection<User> values=dao.list();
        for (User user:values){
            if (user!=null&&user.deptId==deptId){
                inUse=true;
                break;
            }
        }
        return inUse;
    }
@Override
    public boolean roleInUse(int roleId){
        boolean inUse=false;
    Collection<User> values=dao.list();
        for (User user:values){
            if (user!=null&&user.roleId==roleId){
                inUse=true;
                break;
            }
        }
        return inUse;
    }

@Override
    public Result create(UserDto dto){

       if (!deptService.existsById(dto.deptId)){
           return Result.failure("创建失败，不存在指定的部门");
       }
        if (!roleService.existsById(dto.roleId)){
            return Result.failure("创建失败，不存在指定的角色");
        }
   return super.create(dto);
    }
    @Override
    public Result update(UserDto dto,int id){
        if (!deptService.existsById(dto.deptId)){
            return Result.failure("创建失败，不存在指定的部门");
        }
        if (!roleService.existsById(dto.roleId)){
            return Result.failure("创建失败，不存在指定的角色");
        }
  return super.update(dto, id);
    }
    @Override
    public Result toggle(int id){
        if(!existsById(id)){
            return Result.failure("切换状态失败，不存在该数据");
        }User user=dao.findById(id);

        user.enabled=!user.enabled;
        user=dao.save(user);
        return Result.success(user.enabled?"启用成功":"停用成功",null);
    }
}
